import { ElMessageBox } from 'element-plus'
import { computed, ref, watch } from 'vue'
// import { tableList as tl } from '../testData'
import { useModuleStore } from '@/store/moduleStore'
const moduleStore = useModuleStore()
const tl = computed(()=>moduleStore.currentModuleTable)


export default function (fieldSelected: any, tableSelected: any) {
  const actionTypeList = ref([
    { label: '查询', value: 'select' },
    { label: '更新', value: 'update' },
    { label: '删除', value: 'delete' },
    { label: '插入', value: 'insert' }
  ])
  const tableAssociationList = [
    { label: 'INNER JOIN', value: 'INNER JOIN' },
    { label: 'LEFT JOIN', value: 'LEFT JOIN' },
    { label: 'RIGHT JOIN', value: 'RIGHT JOIN' },
    { label: 'FULL JOIN', value: 'FULL JOIN' },
    { label: 'CROSS JOIN', value: 'CROSS JOIN' },
    { label: '隐式连接', value: 'IMPLICIT JOIN' }
  ]
  const isMutilTable = ref(false)
  const tableSelectAssociation = ref([] as any[])
  function addTableAssociation () {
    tableSelectAssociation.value.push({
      tableA: '',
      fieldA: '',
      association: 'IMPLICIT JOIN',
      tableB: '',
      fieldB: '',
      condition: ''
    })
  }
  const tablesRelations = ref([]) //表关联数组
  // 表选择
  const selectedTableFields = ref([] as any)
  /**
   * 生成查询字段选择列表
   *
   * 遍历已选表数组，检查每个表是否已经在字段选择列表中。
   * 如果不存在，则将该表添加到字段选择列表中，并初始化相关属性。
   * 同时，遍历字段选择列表，移除不在已选表数组中的表，以保持字段选择列表与已选表数组的一致性。
   */
  function generateQueryFieldSelectList () {
    let e = tableSelected.value
    for (let i = 0; i < e.length; i++) {
      let hasThisTable = false
      for (let j = 0; j < fieldSelected.value.length; j++) {
        if (fieldSelected.value[j].table == e[i]) {
          hasThisTable = true
        }
      }
      // 没有这个表，添加
      if (!hasThisTable) {
        fieldSelected.value.push({
          table: e[i],
          isSelectAllField: false,
          fields: [],
          fieldAliasNameMapArray: [],
          isExpand: false
        })
      }
      //去除多余的表
      let notExitTable = []
      for (let j = 0; j < fieldSelected.value.length; j++) {
        if (!e.includes(fieldSelected.value[j].table as never)) {
          notExitTable.push(fieldSelected.value[j].table)
        }
      }
      fieldSelected.value = fieldSelected.value.filter(
        (item: any) => !notExitTable.includes(item.table)
      )
    }
  }

  /**
   * 删除表格关联
   *
   * @param index 要删除的关联在数组中的索引
   */
  function deleteTableAssociation (index: number) {
    ElMessageBox.confirm('确定删除此关联吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
      .then(() => {
        tableSelectAssociation.value.splice(index, 1)
      })
      .catch(() => {})
  }

  /**
   * 选择关联表
   *
   * @param e 事件对象
   */
  function selectAssociationTable (e: any) {
    //计算所有选中的table
    let allTable = [] as any[]
    tableSelectAssociation.value.forEach((item: any) => {
      if (!allTable.includes(item.tableA) && item.tableA) {
        allTable.push(item.tableA)
      }
      if (!allTable.includes(item.tableB) && item.tableB) {
        allTable.push(item.tableB)
      }
    })
    tableSelected.value = allTable as never[]
    selectedTableFields.value = []
    tableSelected.value.forEach((item: any) => {
      if (item) {
        let tlItem = tl.value.find((i: any) => i.value == item) as any
        selectedTableFields.value.push({
          value: item,
          label: tlItem.label,
          children: tlItem && tlItem.fields
        } as never)
      }
    })
    generateQueryFieldSelectList()
  }
  watch(
    () => isMutilTable.value,
    (val: any) => {
      if (!val) {
        tableSelectAssociation.value = [
          {
            tableA: '',
            fieldA: '',
            association: '',
            tableB: '',
            fieldB: '',
            condition: ''
          }
        ]
      } else {
        tableSelectAssociation.value = []
      }
    },
    {
      immediate: true
    }
  )

  return {
    tableSelectAssociation,
    addTableAssociation,
    tableSelected,
    selectedTableFields,
    deleteTableAssociation,
    selectAssociationTable,
    isMutilTable,
    tableAssociationList,
    actionTypeList
  }
}
